package com.wang.system.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author wang
 * @Date 2024/9/22 18:59
 * @PackageName:com.wang.system.config
 * @ClassName: WebMvcConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private TenantInterceptor tenantInterceptor;

    @Autowired
    private JwtAuthenticationInterceptor jwtAuthenticationInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")  // 允许所有域名访问
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册多租户拦截器
        registry.addInterceptor(tenantInterceptor)
                .addPathPatterns("/**");  // 多租户拦截所有请求

        // 注册 JWT 拦截器，排除登录、注册和 Swagger 文档的路径
        registry.addInterceptor(jwtAuthenticationInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/**/login",      // 通配符匹配所有层级的登录路径
                        "/**/register",   // 通配符匹配所有层级的注册路径
                        "/swagger-ui/**",
                        "/doc.html/**",
                        "/v2/api-docs",
                        "/swagger-resources/**");
    }


}